<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" left-arrow @click-left="$router.back()"
      title="黑马头条"></van-nav-bar>
    <!-- /导航栏 -->

    <div class="main-wrap">
      <!-- 加载中 -->
      <div v-if="loading" class="loading-wrap">
        <van-loading color="#3296fa" vertical>加载中</van-loading>
      </div>
      <!-- /加载中 -->

      <!-- 加载完成-文章详情 -->
      <div v-else-if="article.articleTitle" class="article-detail">
        <!-- 文章标题 -->
        <h1 class="article-title">{{ article.articleTitle }}</h1>
        <!-- /文章标题 -->

        <!-- 用户信息 -->
        <van-cell class="user-info" center :border="false">
          <van-image class="avatar" slot="icon" round fit="cover"
            :src="publisher.photo || '../../assets/logo.png'" />
          <div slot="title" class="user-name">{{publisher.username}}</div>
          <div slot="label" class="publish-date">
            {{article.articleCreateDate | relativeTime}}</div>
          <!-- 模板中的$event 是时间参数 -->
          <!-- 当我们传递给子组件的数据既要使用还要修改
            传递：props
            :is-followed="article.is_followed"
            修改：自定义事件
            @update-is_followed="article.is_followed = $event"
            简写方式：在组件上使用v-model
            value=“article.is_followed"
            @input="article.is_followed = $event"
             -->
          <follow-user class="follow-btn" v-model="article.is_followed"
            :user-id="article.articlePublisherId" />
        </van-cell>
        <!-- /用户信息 -->

        <!-- 文章内容 -->
        <div class="article-content markdown-body" v-html="article.articleContext"
          ref="article-content"></div>
        <van-divider>正文结束</van-divider>
        <!-- 文章评论列表 -->
        <comment-list
         :source="article.articleId"
         :list="commentList"
         @onload-success="totalCommentCount=$event.total_count"
         @reply-click="onReplyClick"
         />
        <!-- /文章评论列表 -->
        <!-- 底部区域 -->
        <div class="article-bottom">
          <van-button
           class="comment-btn"
            type="default"
             round size="small"
             @click="isPostShow = true"
             >写评论
          </van-button>
          <van-icon class="comment-icon" name="comment-o" :badge="totalCommentCount" />
          <collect-article class="btn-item" :article-id="article.articleId"
            v-model="article.is_collected" />
          <like-article class="btn-item" v-model="article.is_like"
            :article-id="article.articleId" />
          <van-icon name="share" color="#777777"></van-icon>
        </div>
        <!-- /底部区域 -->
        <!-- 发布评论 -->
        <van-popup
        v-model="isPostShow"
        position="bottom"
        >
         <comment-post
         :target="article.art_id"
        @post-success="onPostSuccess"
          />
        </van-popup>
        <!-- /发布评论 -->
      </div>
      <!-- /加载完成-文章详情 -->

      <!-- 加载失败：404 -->
      <div v-else-if="errStatus === 404" class="error-wrap">
        <van-icon name="failure" />
        <p class="text">该资源不存在或已删除！</p>
      </div>
      <!-- /加载失败：404 -->

      <!-- 加载失败：其它未知错误（例如网络原因或服务端异常） -->
      <div v-else class="error-wrap">
        <van-icon name="failure" />
        <p class="text">内容加载失败！</p>
        <van-button class="retry-btn" @click="loadArticle">点击重试
        </van-button>
      </div>
      <!-- /加载失败：其它未知错误（例如网络原因或服务端异常） -->
    </div>

    <!-- 评论回复 -->
    <!-- 弹出层是懒渲染的：只有在第一次展示的时候才会渲染里面的内容， 之后它的关闭和显示都是在切换内容的显示和隐藏 -->
      <van-popup
    v-model="isReplyShow"
    position="bottom"
    style="height:100%"
     >
     <!-- v-if 条件渲染
     true：渲染元素节点
     false：不渲染
      -->
    <comment-reply
    v-if="isReplyShow"
    :comment="currentComment"
    @close="isReplyShow=false"
     />
</van-popup>
    <!-- /评论回复 -->
  </div>
</template>

<script>
import { getArticleById } from '@/api/article.js'
import { getUserProfile } from "@/api/user.js"
import { ImagePreview } from 'vant'
import FollowUser from '@/components/follow-user'
import CollectArticle from '@/components/collect-article'
import LikeArticle from '@/components/like-article'
import CommentList from './components/comment-list'
import CommentPost from './components/comment-post'
import CommentReply from './components/comment-reply'

export default {
  name: 'ArticleIndex',
  components: {
    FollowUser,
    CollectArticle,
    LikeArticle,
    CommentList,
    CommentPost,
    CommentReply
  },
  // 给所有的后代组件提供数据
  // 然后在任何后代组件里，我们都可以使用 inject 选项来接收指定的我们想要添加在这个实例上的属性
  provide: function () {
    return {
      articleId: this.articleId
    }
  },
  props: {
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      article: {}, // 文章详情
      loading: true, // 加载中的loading状态
      errStatus: 0, // 失败的状态码
      followLoading: false,
      totalCommentCount: 0,
      isPostShow: false, // 控制发布评论的显示状态
      commentList: [], // 评论列表
      isReplyShow: false,
      currentComment: {}, // 当前点击回复的评论项
      publisher: {
        gender: "",
        id: 0,
        phone: "",
        photo: "",
        username: "",
      }
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadArticle()
  },
  mounted () { },
  methods: {
    async loadArticle () {
      // 展示 loading 加载中
      this.loading = true
      try {
        const { data } = await getArticleById(this.articleId)
        // 随机错误
        // if (Math.random() > 0.5) {
        // JSON.parse('dsankljdnskaljndlkjsa')
        // }
        // console.log(data)

        // 数据驱动试图这件事不是立即的
        this.article = data.data
        this.article.is_followed = false
        this.article.is_collected = false
        this.article.is_like = 0

        // 初始化图片点击预览
        // console.log(this.$refs['article-content'])
        setTimeout(() => {
          // this.previewImage()
          this.getUserProfile()
        }, 0)

        // 请求成功，关闭loading
        // this.loading = false
      } catch (err) {
        if (err.response && err.response.status === 404) {
          this.errStatus = 404
        }
        console.log('抱歉请求出错', err)
      }
      // 无论成功还是失败都要关闭loading
      this.loading = false
    },
    previewImage () {
      // 得到所有的img节点
      const articleContent = this.$refs['article-content']
      console.log(articleContent)
      const imgs = articleContent.querySelectorAll('img')
      if (imgs) {
        // 获取所有 img 地址
        const images = []
        imgs.forEach((img, index) => {
          images.push(img.src)
          // 给每个img注册点击事件，在处理函数中调用预览
          img.onclick = () => {
            ImagePreview({
              // 预览的图片地址数组
              images,
              // 预览图片的起始位置
              startPosition: index
            })
          }
        })
      }
    },
    onPostSuccess (data) {
    // 关闭发布评论弹层
      this.isPostShow = false
      // 将发布内容显示到列表顶部
      this.commentList.unshift(data.new_obj)
    },
    onReplyClick (comment) {
      this.currentComment = comment
      // 显示评论回复弹出层
      this.isReplyShow = true
    },
    async getUserProfile() {
      const {data} = await getUserProfile(this.article.articlePublisherId)
      this.publisher = data
    }
  }
}
</script>

<style scoped lang="less">
@import "./github-markdown.css";

.article-container {
  .main-wrap {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: #fff;
  }
  .article-detail {
    position: fixed;
    left: 0;
    right: 0;
    top: 92px;
    bottom: 88px;
    overflow-y: scroll;
    background-color: #fff;
    .article-title {
      font-size: 40px;
      padding: 50px 32px;
      margin: 0;
      color: #3a3a3a;
    }

    .user-info {
      padding: 0 32px;
      .avatar {
        width: 70px;
        height: 70px;
        margin-right: 17px;
      }
      .van-cell__label {
        margin-top: 0;
      }
      .user-name {
        font-size: 24px;
        color: #3a3a3a;
      }
      .publish-date {
        font-size: 23px;
        color: #b7b7b7;
      }
      .follow-btn {
        width: 170px;
        height: 58px;
      }
    }

    .article-content {
      padding: 55px 32px;
      /deep/ p {
        text-align: justify;
      }
    }
  }

  .loading-wrap {
    padding: 200px 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
  }

  .error-wrap {
    padding: 200px 32px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background-color: #fff;
    .van-icon {
      font-size: 122px;
      color: #b4b4b4;
    }
    .text {
      font-size: 30px;
      color: #666666;
      margin: 33px 0 46px;
    }
    .retry-btn {
      width: 280px;
      height: 70px;
      line-height: 70px;
      border: 1px solid #c3c3c3;
      font-size: 30px;
      color: #666666;
    }
  }

  .article-bottom {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: space-around;
    align-items: center;
    box-sizing: border-box;
    height: 88px;
    border-top: 1px solid #d8d8d8;
    background-color: #fff;
    .comment-btn {
      width: 282px;
      height: 46px;
      border: 2px solid #eeeeee;
      font-size: 30px;
      line-height: 46px;
      color: #a7a7a7;
    }
    /deep/ .van-icon {
      font-size: 40px;
    }
    .comment-icon {
      top: 2px;
      color: #777;
      .van-info {
        font-size: 16px;
        background-color: #e22829;
      }
    }
    .btn-item {
      border: none;
      padding: 0;
      height: 40px;
      line-height: 40px;
      color: #777777;
    }
    .collect-btn--collected {
      color: #ffa500;
    }
    .like-btn--liked {
      color: #e5645f;
    }
  }
}
</style>
